/**
 * Copyright (c) 2024 Hunan OpenValley Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "GPUImageSharpenFilter.h"

const std::string GPUImageSharpenFilter::SHARPEN_VERTEX_SHADER = R"(
    attribute vec4 position;
    attribute vec4 inputTextureCoordinate;
    
    uniform float imageWidthFactor; 
    uniform float imageHeightFactor; 
    uniform float sharpness;
    
    varying vec2 textureCoordinate;
    varying vec2 leftTextureCoordinate;
    varying vec2 rightTextureCoordinate; 
    varying vec2 topTextureCoordinate;
    varying vec2 bottomTextureCoordinate;
    
    varying float centerMultiplier;
    varying float edgeMultiplier;
    
    void main()
    {
        gl_Position = position;
        mediump vec2 widthStep = vec2(imageWidthFactor, 0.0);
        mediump vec2 heightStep = vec2(0.0, imageHeightFactor);
        textureCoordinate = inputTextureCoordinate.xy;
        leftTextureCoordinate = inputTextureCoordinate.xy - widthStep;
        rightTextureCoordinate = inputTextureCoordinate.xy + widthStep;
        topTextureCoordinate = inputTextureCoordinate.xy + heightStep;     
        bottomTextureCoordinate = inputTextureCoordinate.xy - heightStep;
        
        centerMultiplier = 1.0 + 4.0 * sharpness;
        edgeMultiplier = sharpness;
    }
)";

const std::string GPUImageSharpenFilter::SHARPEN_FRAGMENT_SHADER = R"(
    #extension GL_OES_EGL_image_external : require
    precision highp float;
    
    varying highp vec2 textureCoordinate;
    varying highp vec2 leftTextureCoordinate;
    varying highp vec2 rightTextureCoordinate; 
    varying highp vec2 topTextureCoordinate;
    varying highp vec2 bottomTextureCoordinate;
    
    varying highp float centerMultiplier;
    varying highp float edgeMultiplier;
    
    uniform samplerExternalOES inputImageTexture;
    
    void main()
    {
        mediump vec3 textureColor = texture2D(inputImageTexture, textureCoordinate).rgb;
        mediump vec3 leftTextureColor = texture2D(inputImageTexture, leftTextureCoordinate).rgb;
        mediump vec3 rightTextureColor = texture2D(inputImageTexture, rightTextureCoordinate).rgb;
        mediump vec3 topTextureColor = texture2D(inputImageTexture, topTextureCoordinate).rgb;
        mediump vec3 bottomTextureColor = texture2D(inputImageTexture, bottomTextureCoordinate).rgb;
        
        gl_FragColor = vec4((textureColor * centerMultiplier - (leftTextureColor * edgeMultiplier + rightTextureColor * edgeMultiplier + topTextureColor * edgeMultiplier + bottomTextureColor * edgeMultiplier)), texture2D(inputImageTexture, bottomTextureCoordinate).w);
    }
)";

GPUImageSharpenFilter::GPUImageSharpenFilter() : sharpness(0.0f) {}

GPUImageSharpenFilter::GPUImageSharpenFilter(const float sharpVal)
    : GPUImageFilter(SHARPEN_VERTEX_SHADER, SHARPEN_FRAGMENT_SHADER)
{
    sharpness = sharpVal;
}

void GPUImageSharpenFilter::onInit()
{
    GPUImageFilter::onInit();
    sharpnessLocation = glGetUniformLocation(getProgram(), "sharpness");
    imageWidthFactorLocation =
        glGetUniformLocation(getProgram(), "imageWidthFactor");
    imageHeightFactorLocation =
        glGetUniformLocation(getProgram(), "imageHeightFactor");
}

void GPUImageSharpenFilter::onInitialized()
{
    GPUImageFilter::onInitialized();
    setSharpness(sharpness);
}

void GPUImageSharpenFilter::onOutputSizeChanged(int width, int height)
{
    if (width == 0 || height == 0) {
        return;
    }
    GPUImageFilter::onOutputSizeChanged(width, height);
    setFloat(imageWidthFactorLocation, 1.0f / width);
    setFloat(imageHeightFactorLocation, 1.0f / height);
}

void GPUImageSharpenFilter::setSharpness(const float sharpVal)
{
    sharpness = sharpVal;
    setFloat(sharpnessLocation, sharpVal);
}
